package in.muton.edushare.components.messaging.sendpanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.api.ScheduleMessage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.schedules.DateParser;
import in.muton.edushare.schedules.NewSchedule;

import java.text.ParseException;
import java.util.Date;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Schedules;

public class SchedulePanel extends EdusharePanel{
	private static final long serialVersionUID = 5661748665743592418L;

	public SchedulePanel(String id, IModel<? extends Object> pageOwner,
			int type, int oId, EduShareBasePage page) {
		super(id, page);
		this.id = oId;
		this.type = type;
		this.obj = pageOwner;
		schedulePanel();
	}

	public Form<Void> schedulePanel() {
		Form<Void> f = new Form<Void>("ScheduleFormSchPanel") {
			@Override
			public void onSubmit() {
				Schedules s = new Schedules();
				if (!StringUtil.isValid(nameEvtSchPanel)) {
					setError("Event title is required.");
					return;
				}
				try {
					s.setSchStart(DateParser.getDate(startTimeSchPanel));
					s.setSchEnd(DateParser.getDate(endTimeSchPanel));
					if (s.getSchStart().getTime() > s.getSchEnd().getTime()) {
						setError("Start time must be less than end time.");
						return;
					}
					if(s.getSchStart().getTime() < new Date().getTime()){
						setError("The event must start in the future.");
						return;
					}

					s.setSchTitle(nameEvtSchPanel);
					s.setSchLocation(locationSchPanel);
				} catch (ParseException e) {
					info("The start/end date fields are incorrect.");
					return;
				}
				s.setCreatingUser(EduShareSession.get().getLoggedInUser());
				s.setCreationTimestamp(new Date());
				s.setUpdatedTimestamp(new Date());
				s.setStatus(Schedules.EVENT_STATUS_OPEN);
				new NewSchedule(s, obj.getObject(), type, id).send();
				new ScheduleMessage(s, ScheduleMessage.NEW_EVENT).setReciever(
						obj.getObject()).setPrivacy(
						Messages.PRIVACY_TYPE_PUBLIC).send();
				nameEvtSchPanel = "";
				locationSchPanel = "";
				startTimeSchPanel = "";
				endTimeSchPanel = "";
			}

			private static final long serialVersionUID = 8482980712087467876L;
		};
		add(f);
		f.setOutputMarkupId(true).setMarkupId("ScheduleFormSchPanel");
		setDefaultModel(new CompoundPropertyModel<SchedulePanel>(this));
		f.add(new TextField<String>("nameEvtSchPanel").setOutputMarkupId(true)
				.setMarkupId("nameSPCat"));
		f.add(new TextField<String>("locationSchPanel"));
		f.add(new HiddenField<String>("startTimeSchPanel").setOutputMarkupId(
				true).setMarkupId("startTimeSPCat"));
		f.add(new HiddenField<String>("endTimeSchPanel")
				.setOutputMarkupId(true).setMarkupId("endTimeSPCat"));
		return f;
	}

	public String nameEvtSchPanel = "";
	public String locationSchPanel = "";
	public String startTimeSchPanel = "";
	public String endTimeSchPanel = "";

	private int id = 0;
	private int type = 0;
	private IModel<? extends Object> obj = null;
}
